package net.atomarrow.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
/**
 * Bean工具类
 * @author Michael
 *
 */
public class BeanUtil {

    /**
     * 获取值
     * @param fieldName
     * @param object
     * @return
     */
    public static Object getValue(String fieldName, Object object) {
        Field field;
        Object result = null;
        try {
            field = object.getClass().getDeclaredField(fieldName);
            makeAccessible(field);
            result = field.get(object);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 设置值
     * @param object
     * @param fieldName
     * @param value
     */
    public static void setFieldValue(final Object object,
            final String fieldName, final Object value) {
        Field field = null;
        try {
            field = object.getClass().getDeclaredField(fieldName);
            makeAccessible(field);
            field.set(object, value);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
    }

    protected static void makeAccessible(final Field field) {
        if (!Modifier.isPublic(field.getModifiers())
                || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
            field.setAccessible(true);
        }
    }

    /**
     * 使用get方法方式获取值
     * @param bean
     * @param propName
     * @return
     * @throws IllegalArgumentException
     * @throws SecurityException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws NoSuchMethodException
     */
    public static Object getSimpleProperty(Object bean, String propName){
        try {
			return bean.getClass().getMethod(getReadMethod(propName)).invoke(bean);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		}
        return null;
    }
    
    /**
     * 使用set方法赋值
     * @param obj
     * @param field
     * @param value
     */
    public static void setValueByMethod(Object obj,Field field,Object value){
        try {
            obj.getClass().getMethod(getSetMethod(field.getName()), field.getType()).invoke(obj, value);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private static String getReadMethod(String name) {
        return "get" + StringUtil.toUpperCaseFirstWord(name);
    }
    
    
    private static String getSetMethod(String name) {
        return "set" + StringUtil.toUpperCaseFirstWord(name);
    }
    /**
     * 克隆第一个对象到第二个对象
     * @param src
     * @param dest
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public static void clone(Object src, Object dest)
            throws IllegalArgumentException, IllegalAccessException {
        Class clazz = src.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if(Modifier.isStatic(field.getModifiers())){
                continue;
            }
            field.setAccessible(true);
            field.set(dest, field.get(src));
        }
    }

}
